home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / shell.src < prev    next >
Text File  |  1991-04-20  |  15KB  |  342 lines

  1. %%HP: T(3)A(R)F(.);
  2. @ SHELL, by Aaron Boonshoft, 21 April 1991
  3. @ Posted to comp.sys.handhelds, hpcvbbs item # 2863
  4. DIR
  5.  
  6. @ Hi, I recently graduated from The Ohio State University and moved to
  7. @ Corvallis Oregon to pursue a career in handheld computing.  As I
  8. @ learn more about programming these devices, I have come to realize some
  9. @ things about user interfaces.  The following summarizes some of what I
  10. @ have learned.  I hope it is helpful.
  11.  
  12. @*********************************************************************
  13.  
  14. @                 +===========================+
  15. @                 | HP48 SCIENTIFIC EXPANDABLE|
  16. @       A         |+-------------------------+|   Hi!  How are you?
  17. @                 ||   <( O )>     <( O )>   ||             
  18. @                 ||                         ||         /
  19. @    FRIENDLY     || [  ]               [  ] ||  ______/
  20. @                 ||    [  ][  ] [  ][  ]    ||
  21. @                 |+-------------------------+|
  22. @     HP 48       |===========================|
  23. @                 || [A] [B] [C] [D] [E] [F] ||
  24. @                 || MTH PRG CST VAR [^] NXT ||
  25. @      USER       || ['] STO EVL [<] [v] [>] ||          by
  26. @                 || SIN COS TAN SQR Y^X 1/X ||
  27. @                 || [ENTER] +/- EEX DEL [<= ||
  28. @     INTER-      || [a]  [7]  [8]  [9]  [/] ||    Aaron Boonshoft
  29. @                 || [<]  [4]  [5]  [6]  [X] ||
  30. @                 || [>]  [1]  [2]  [3]  [-] ||
  31. @      FACE       || [ON  [0]  [.]  SPC  [+] ||
  32. @                 +===========================+
  33.  
  34. @ Introduction:
  35.  
  36. @ One of the more challenging aspects of writing professional software
  37. @ for the HP 48 is the user interface.  Professional software normally
  38. @ requires a very clean, friendly, and flexible user interface.
  39. @ The software should not generate an error regardless of the keys the
  40. @ user presses (including [ATTN]) or of the input data the user enters
  41. @ (including total gibberish).  The user interface is often time
  42. @ consuming to design and debug.  It may also account for more
  43. @ than half of the code in a project.  One way of combating the
  44. @ problems of writing user interface code is to write the code in the
  45. @ form of subprograms (programs designed to be used by other 
  46. @ programs).  In this form, the code may be used for many different
  47. @ projects.  This may greatly reduce their size and development time.
  48.  
  49. @ Below are five sets of programs and subprograms.  The first set
  50. @ just contains examples of how the other four are used.  Each of the
  51. @ other four contain one major subprogram which, in some cases, uses
  52. @ the help of supporting subprograms or other major subprograms.  The 
  53. @ subprograms take care of some general I/O needs and the inputting of
  54. @ real numbers.  They are not designed to handle all I/O.
  55.  
  56. @ * BOXV is for demonstration only.  It directly or indirectly
  57. @     makes use of all the other sets.
  58. @ * RUN makes interfaces cleaner by taking program objects off the
  59. @     stack if the [ATTN] key is pressed.  It will also return the
  60. @     user to the menu used before the program was run.
  61. @ * IPMO adds flexibility by allowing the user to choose a menu option
  62. @     or the stack at an INPUT prompt.  It makes use of PAUSE.
  63. @ * CIA makes interfaces more robust by checking for bad input of real
  64. @     numbers and telling the user what's wrong.  It will also do
  65. @     conversions with unit objects (including ones that have the
  66. @     underscore missing.)
  67. @ * PAUSE uses an alarm to halt program execution for any number of
  68. @     seconds.  It can also be used to update the stack display.
  69.  
  70. @ (NOTE: Subprogram EB in the RUN set is also used by IPMO and CIA)
  71.  
  72. @ All of the subprograms have been written as straightforwardly as
  73. @ possible to allow for modifications.  The only programming trick
  74. @ that is used is to store some objects as strings.  This technique
  75. @ was used four times in IPMO to save about 210 bytes.  Note, however,
  76. @ that doing this makes the code less obvious and slower to a small
  77. @ degree.
  78.  
  79. @*********************************************************************
  80.  
  81. @ BOXV: Box Volume Example
  82.  
  83. @ BV  is just an example program for showing how IPMO and CIA can be
  84. @ used.  BOXV uses RUN to execute BV.  BOXV or BV will ask the user
  85. @ to input the volume of a box or it will CALC'ulate it from the
  86. @ dimensions.  A volume of 1.2_ft^3 is used to demonstrate the use of
  87. @ default values.  To really see why the user interface can be called
  88. @ professional, try entering negative numbers or gibberish, entering
  89. @ 3ft^3 instead of 3_ft^3 or pressing the ATTN key while running BOXV.
  90. @ Executing ERUN demonstrates how RUN deals with an error caused by
  91. @ something other than pressing the ATTN key. See the sections below
  92. @ this one for more information about RUN, IPMO and CIA.
  93.  
  94. BOXV @ Compute Box Volume ( --> box_volume )       BYTES # 61780d 35.5
  95. \<< 'BV' RUN \>>
  96.  
  97. BV @ Subprogram for BOXV  ( --> box_volume )        BYTES # 57289d 419
  98. \<<
  99. DO
  100.  "Box Volume?" ":V:" "1.2_ft^3" 45 {"CALC"} IPMO
  101.  IF THEN
  102.   43 {1_ft 'x > 0'} \-> M C \<<
  103.   DO M TMENU "Box Width?"  ":W:" INPUT C CIA UNTIL END
  104.   DO M TMENU "Box Depth?"  ":D:" INPUT C CIA UNTIL END
  105.   DO M TMENU "Box Height?" ":H:" INPUT C CIA UNTIL END
  106.   * * 1 \>>
  107.  ELSE { 1_ft^3 'x > 0'} CIA END
  108. UNTIL END 1_ft^3 * "V" \->TAG 
  109. \>>
  110.  
  111. ERUN @ Example of RUN catching an error  ( --> )     BYTES # 31101d 40
  112. @ Note that a list can be evaluated.
  113. \<< { "" 2 * } RUN \>>
  114.  
  115. @*********************************************************************
  116.  
  117. @ RUN: Evaluate Object and Trap Errors
  118.  
  119. @ RUN is designed to evaluate programs and other objects.  If the [ATTN]
  120. @ key is pressed while using RUN, the stack will be reset to the input
  121. @ stack.  Unlike the LASTSTACK operation, it does not require the user
  122. @ to press any additional keys or have any particular flag setting.
  123. @ Using RUN will bring the back the menu and flag settings used before
  124. @ the program started.
  125.  
  126. @ Note that there are some cases where pressing the [ATTN] key may not
  127. @ be caught by RUN:
  128. @ * If [ATTN] is pressed "immediately" after starting RUN
  129. @ * If [ATTN] is pressed two are more times "rapidly"
  130. @ * If the program being evaluated by RUN contains an IFERR command
  131.  
  132. @ RUN STACK INPUT
  133. @   Level 1: Object to be evaluated by RUN
  134. @    (other levels may contain input for the object)
  135. @ RUN STACK OUTPUT
  136. @   If [ATTN] was "not" pressed, the stack will contain the output of
  137. @    the object evaluated by RUN.
  138. @   If [ATTN] was pressed or the object evaluated with an error,
  139. @    the stack will be reset to the input stack minus level 1.
  140.  
  141. RUN @ Evaluate Object                               BYTES # 57031d 276
  142. \<< 
  143. DEPTH ROLLD DEPTH 1 - \->LIST RCLMENU RCLF \-> prg stk men flg
  144. \<<
  145. IFERR stk LIST\-> DROP prg EVAL THEN 
  146. IF ERRN #0d SAME NOT THEN
  147.  CLLCD "An error has occurred" 1 DISP "while evaluating..." 2 DISP
  148.  prg 3 DISP ERRN 5 DISP ERRM 6 DISP EB 3 FREEZE END
  149. DEPTH DROPN stk LIST\-> DROP END
  150. flg STOF men TMENU
  151. \>> \>>
  152.  
  153. EB @ Error Beep ( --> )                            BYTES # 32606d 33.5
  154. \<< "1400 .1" STR\-> BEEP \>>
  155.  
  156. @*********************************************************************
  157.  
  158. @ IPMO: INPUT with MENU Options
  159.  
  160. @ IPMO may be used instead of (really in addition to) the built-in
  161. @ INPUT command.   It allows the user to choose a menu option rather
  162. @ than inputting the requested data.  It also allows the user to
  163. @ access an empty stack to compute the input if necessary.
  164.  
  165. @ IPMO STACK INPUT
  166. @   Level 5: INPUT command "stack prompt" (3 lines or less)
  167. @   Level 4: INPUT command "command-line prompt" (list not allowed)
  168. @   Level 3: Default value, any object, (should be "" if no default)
  169. @   Level 2: Menu displayed during stack INPUT (real number or list)
  170. @   Level 1: Menu displayed during menu INPUT (must be a list)
  171.  
  172. @ IPMO STACK OUTPUT
  173. @   Level 2: INPUT command "result" if 0 in level 1
  174. @   Level 1: Exit Condition, 0 for INPUT, 1 to 6 for menu key
  175.  
  176. @ When the user is asked for input by IPMO, one of five things happens
  177. @ depending on which key is pressed first:
  178.  
  179. @   ( 1.)  [ENTER] key will cause IPMO to end.  The default value and
  180. @   a 0 will be the stack output.
  181.  
  182. @   ( 2.)  [0],[1],[2],...,[9],[.],backspace or [EXX] keys will cause
  183. @   the 2nd level menu to be displayed and the real INPUT command will
  184. @   be executed with the appropriate data appended to the command line
  185. @   prompt.  The default, if any, will vanish from the display.  The
  186. @   "result" from INPUT and the number 0 will be the stack output.
  187.  
  188. @   ( 3.)  Any menu key that is not blank will cause IPMO to end.
  189. @   A number from 1 to 6 corresponding to the menu key (going left to
  190. @   right) will be the stack output.
  191.  
  192. @   ( 4.)  [CONT] will cause the stack to be saved and IPMO to HALT.
  193. @   An empty stack is then available for the user to compute the
  194. @   required input.  When [CONT] is pressed again, IPMO will make
  195. @   the object in level 1 a string and leave it and 0 on the stack.
  196.  
  197. @   ( 5.)  [ATTN] key will cause an error.  An error trap should be
  198. @   used outside of IPMO.  RUN is recommended.
  199.  
  200. @ Pressing any other key will result in an error tone.  The
  201. @ only exceptions are shifted versions of most of the keys above.
  202. @ Only [CONT] and [ATTN] are shift sensitive.
  203.  
  204. IPMO @ INPUT with MENU Options                    BYTES # 48856d 669.5
  205. \<<
  206. 5 DUPN CLLCD TMENU DROP + 3 \->GROB SWAP 3 DISP
  207. "(Press CONT for stack)" 1 DISP
  208. LCD\-> "{#0d#46d}" STR\-> ROT REPL \->LCD
  209. @sON EN M6 M5 M4 M3 M2 M1 [0][1][2][3][4][5][6][7][8][9][.][<][EXX]
  210. @ -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8  9 10 11 12 13
  211. "{91 51 16 15 14 13 12 11 92 82 83 84 72 73 74 62 63 64 93 55 53}"
  212. STR\->
  213. DO
  214.  DUP -1 WAIT IP POS 8 -
  215.  CASE DUP
  216.   1 \>= THEN C$ 41 {"0""1""2""3""4""5""6""7""8""9"".""""1E"} STR\->
  217.              SWAP GET 5 ROLLD DROP2 TMENU DROP + INPUT 0 1 END DUP
  218.  -5 \>= THEN NEG 1 + 3 PICK C$ 14 {""""""""""""} STR\-> + OVER GET 
  219.              IF "" \=/ THEN 7 ROLLD 6 DROPN 1
  220.              ELSE EB NOT END END DUP
  221.  -6  == THEN 4 DROPN + SWAP DROP 0 1 END
  222.  -7  == THEN DROP2 TMENU 3 DROPN DEPTH \->LIST \-> \255 \<<
  223. 0 PAUSE "Place desired input on
  224. stack level 1 and then
  225. press CONT to continue
  226. running program." 1 DISP 3 FREEZE HALT
  227.              IF DEPTH THEN \->STR DEPTH ROLLD DEPTH 1 - DROPN
  228.              ELSE "" END \255 LIST\-> 1 + ROLL \>> 0 1 END
  229.  EB 0 END
  230. UNTIL END
  231. \>>
  232.  
  233. @*********************************************************************
  234.  
  235. @ CIA: Check Input Acceptability
  236.  
  237. @ CIA will verify that the user has INPUT a valid real number based on
  238. @ a check list.  The real number may be tagged or have units but it
  239. @ must be contained in a string.  CIA will use ERR to display what
  240. @ is wrong to the user if the INPUT value is not acceptable.
  241.  
  242. @ CIA STACK INPUT
  243. @   Level 2: "result", INPUT value
  244. @   Level 1: { unt rng }, check list (see below)
  245. @ CIA STACK OUTPUT
  246. @   Level 2: new_result, if 1 in Stack level 1 (real number)
  247. @   Level 1: OK_flag, 1 if "value" OK, 0 otherwise
  248.  
  249. @ The check list must contain two values:
  250. @   unt = the desired input units.  unt=1 if units will be ignored.
  251. @         If the unt is anything else, the INPUT value will be
  252. @         CONVERT'ed to those units and then the units will be removed.
  253. @         The new_result will be a real number without tags or units.
  254. @  'rng'= an expression which results in 1 if the INPUT value is
  255. @         within range or 0 if not.  The expression must refer to the
  256. @         INPUT value as 'x' as in 'x\>=0' or '2<ABS(x) AND ABS(x)<3'.
  257. @         If rng=1, any INPUT value will be within range.
  258.  
  259. @ Note that you may want to tell the user what units are required (if
  260. @ any) and what range of values are allowable.  This could be done as
  261. @ part of the prompt messages for INPUT or IPMO.
  262.  
  263. @ Also note that during the INPUT command, PRG entry mode is used.
  264. @ This will cause 3_ft^3 to be entered as 3ft^3 unless you explicitly
  265. @ type in "_" character.  CIA is smart enough, with the help of ISU,
  266. @ to recognize and compensate for this in virtually every case.  In
  267. @ other words, 3ft^3 will be treated like 3_ft^3 so the user does not
  268. @ have type the "_" character when using units.
  269.  
  270. CIA @ Check Input Acceptability                   BYTES # 57037d 590.5
  271. \<<
  272. DEPTH SWAP LIST\-> DROP \-> dep unt rng \<<
  273. CASE
  274.  IFERR STR\-> THEN
  275.   ISU IFERR STR\-> THEN 1
  276.  ELSE 0 END ELSE 0 END  THEN DROP "    Invalid Syntax" ERR END
  277.  DEPTH dep - 1 +
  278.  IF DUP 1 == THEN
  279.   ROT DTAG 3 ROLLD
  280.   IF
  281.    3 PICK TYPE 0 ==
  282.    3 PICK TYPE DUP 6 ==
  283.    SWAP 7 == OR AND
  284.   THEN
  285.    DROP _ 0
  286.   END END DUP                THEN 1 + DROPN "Only One Value Allowed"
  287.                                    ERR END DROP
  288.  DTAG DUP TYPE DUP 13 == XOR THEN DROP "Must be a Real Number" ERR END
  289.  unt 1 SAME NOT OVER TYPE AND
  290.   IF THEN IFERR unt CONVERT
  291.    THEN 1 ELSE UVAL 0 END
  292.   ELSE UVAL 0 END            THEN DROP2 "  Inconsistent Units" ERR END
  293.  rng { x } 3 PICK + \|^MATCH
  294.  DROP \->NUM NOT             THEN DROP "  Value Out of Range" ERR END
  295.  1 END
  296. \>> \>>
  297.  
  298. ISU @ Insert Underscore Character                 BYTES # 39323d 196.5
  299. @ ( "ValueUnits" --> "Value_Units" )
  300. \<<
  301.  DUP SIZE 1
  302.  IF DUP2 \>= THEN FOR n
  303.   IF "0123456789" OVER n DUP SUB POS THEN
  304.    IF "^" OVER n 1 - DUP SUB \=/ THEN
  305.     DUP 1 n SUB "_" + SWAP n 1 + MAXR
  306.     \->NUM SUB + 0 'n' STO END END
  307.  -1 STEP ELSE DROP2 END
  308. \>>
  309.  
  310. ERR @ Display Input Error  ( "message" --> 0 )       BYTES # 10754d 87
  311. \<< CLLCD "Sorry, not acceptable:" 3 DISP
  312. 5 DISP {"OK"} TMENU EB -1 WAIT DROP 0 \>>
  313.  
  314. @*********************************************************************
  315.  
  316. @ PAUSE: Timed Program HALT
  317.  
  318. @ PAUSE will set an alarm and HALT.  The alarm will CONT the program
  319. @ after a given number of seconds.  One reason why this is useful is
  320. @ that it allows the calculator time to update the stack display.
  321. @ PAUSE uses ATCT and ADDT to do some date and time arithmetic.  They
  322. @ use HP 48 HMS and Date formats.
  323.  
  324. PAUSE @ Timed Program HALT  ( seconds --> )          BYTES # 19068d 88
  325. \<< .2 MAX 3600 / \->HMS MEM DROP ATCT
  326. \<< DELALARM CONT \>> + STOALARM DROP HALT \>>
  327.  
  328. ATCT @ Add Time to Current Time                    BYTES # 30370d 38.5
  329. @ ( Delta_Time --> {Date2 Time2} )  Computes Time2 and Date2 by adding
  330. @ Delta_Time to the current Time and Date.
  331. \<< DATE TIME 2 \->LIST SWAP ADDT \>>
  332.  
  333. ADDT @ Add Time to a Date and a Time                 BYTES # 50943d 77
  334. @ ( {Date1 Time1} Delta_Time --> {Date2 Time2} )
  335. @ Computes Time2 and Date2 by adding Delta_Time to Time1 and Date1.
  336. \<< SWAP LIST\-> DROP ROT HMS+ DUP 24 / IP
  337. ROT SWAP DATE+ SWAP 24 MOD 2 \->LIST \>>
  338.  
  339. @*********************************************************************
  340.  
  341. END
  342.